package cn.nubia.admin.common.springmvc;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Created by Administrator on 2015/6/26.
 */
public class SpringContextLoader implements ApplicationContextAware {


    // Spring 上下文对象
    private static ApplicationContext context;


    /**
     * 取得spring环境的Bean
     * @param beanName
     * @return
     */
    public static Object getBean(String beanName) {
        return context.getBean(beanName);
    }

    public static <T> T getBean(Class<T> clazz) {
        return context.getBean(clazz);
    }

    public static boolean isBeanExist(String beanname) {
        return context.containsBean(beanname);
    }

    @Override
    public  void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextLoader.context = applicationContext;
    }
}